<?php

namespace CZTools\Hyperf\library;

class Ucpaas
{
    //API请求地址
    const BaseUrl = "https://open.ucpaas.com/ol/sms/";
    //开发者账号ID。由32个英文字母和阿拉伯数字组成的开发者账号唯一标识符。
    private $sid;
    //开发者账号TOKEN
    private $token;
    //应用id
    private $appid;
    //模版id
    private $templateid;

    public function __construct()
    {
    }

    private static function getResult($url, $body = null, $method)
    {
        $data = self::connection($url, $body, $method);
        if (isset($data) && !empty($data)) {
            $result = $data;
        } else {
            $result = '没有返回数据';
        }
        return $result;
    }

    /**
     * @param $url    请求链接
     * @param $body   post数据
     * @param $method post或get
     * @return mixed|string
     */
    private static function connection($url, $body, $method)
    {
        if (function_exists("curl_init")) {
            $header = array(
                'Accept:application/json',
                'Content-Type:application/json;charset=utf-8',
            );
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            if ($method == 'post') {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $opts = array();
            $opts['http'] = array();
            $headers = array(
                "method" => strtoupper($method),
            );
            $headers[] = 'Accept:application/json';
            $headers['header'] = array();
            $headers['header'][] = 'Content-Type:application/json;charset=utf-8';

            if (!empty($body)) {
                $headers['header'][] = 'Content-Length:' . strlen($body);
                $headers['content'] = $body;
            }

            $opts['http'] = $headers;
            $result = file_get_contents($url, false, stream_context_create($opts));
        }
        return @json_decode($result, true);
    }

    /**
     * 单条发送短信的function，适用于注册/找回密码/认证/操作提醒等单个用户单条短信的发送场景
     * @param int $mobile       接收短信的手机号码
     * @param string|null $param 变量参数，多个参数使用英文逗号隔开（如：param=“a,b,c”）
     * @param int $templateid   短信模板，可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
     * @param string $uid       用于贵司标识短信的参数，按需选填。
     * @return mixed|string
     * @throws Exception
     */
    public static function SendSms(int $mobile, int $templateid, string $param, int $uid = 0)
    {
        $url = self::BaseUrl . 'sendsms';
        $body_json = array(
            'sid' => config('cloud_message.sid'),
            'token' => config('cloud_message.token'),
            'appid' => config('cloud_message.appid'),
            'mobile' => $mobile,
            'templateid' => $templateid,
            'param' => $param,
            'uid' => $uid,
        );
        $body = json_encode($body_json);
        $data = self::getResult($url, $body, 'post');
        return $data;
    }

    /**
     * 群发送短信的function，适用于运营/告警/批量通知等多用户的发送场景
     * @param string $mobileList    接收短信的手机号码，多个号码将用英文逗号隔开，如“18088888888,15055555555,13100000000”
     * @param int $templateid       短信模板，可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
     * @param int $param            变量参数，多个参数使用英文逗号隔开（如：param=“a,b,c”）
     * @param string $uid           用于贵司标识短信的参数，按需选填。
     * @return mixed|string
     * @throws Exception
     */
    public static function SendSms_Batch(string $mobileList, int $param , int $templateid, string $uid = '')
    {
        $url = self::BaseUrl . 'sendsms_batch';

        $body = json_encode([
            'sid' => config('cloud_message.sid'),
            'token' => config('cloud_message.token'),
            'appid' => config('cloud_message.appid'),
            'templateid' => $templateid,
            'param' => $param,
            'mobile' => $mobileList,
            'uid' => $uid ? $uid : 0,
        ]);
        $data = self::getResult($url, $body, 'post');
        return $data;
    }
}
